package com.driver.manage.controller;

import com.alibaba.fastjson2.JSONObject;
import com.driver.manage.ocr.DetectFace;
import com.driver.manage.ocr.Format;
import com.driver.manage.ocr.ImageRecognize;
import com.ruoyi.common.core.domain.R;
import okhttp3.ResponseBody;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.IOException;
import java.util.HashMap;

@RestController
@RequestMapping("approval")
public class CheckController {

    @PostMapping("uploadIdCard")
    public R uploadIdCard(MultipartFile multipartFile){
        String requestUrl = "https://aip.baidubce.com/rest/2.0/ocr/v1/idcard";
        String fileName = multipartFile.getOriginalFilename();
        String prefix = fileName.substring(fileName.lastIndexOf("."));
        try {
            File file = File.createTempFile(fileName, prefix);
            multipartFile.transferTo(file);
            String recognize = ImageRecognize.recognize(requestUrl, file);
            HashMap map = Format.idcard(recognize);
            return R.ok(map);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

    @PostMapping("uploadDriving")
    public R uploadDriving(MultipartFile multipartFile){
        String requestUrl = "https://aip.baidubce.com/rest/2.0/ocr/v1/driving_license";
        String fileName = multipartFile.getOriginalFilename();
        String prefix = fileName.substring(fileName.lastIndexOf("."));
        try {
            File file = File.createTempFile(fileName, prefix);
            multipartFile.transferTo(file);
            String recognize = ImageRecognize.recognize(requestUrl, file);
            HashMap map = Format.DrivingLicense(recognize);
            return R.ok(map);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

    @PostMapping("uploadVehicle")
    public R uploadVehicle(MultipartFile multipartFile){
        String requestUrl = "https://aip.baidubce.com/rest/2.0/ocr/v1/vehicle_license";
        String fileName = multipartFile.getOriginalFilename();
        String prefix = fileName.substring(fileName.lastIndexOf("."));
        try {
            File file = File.createTempFile(fileName, prefix);
            multipartFile.transferTo(file);
            String recognize = ImageRecognize.recognize(requestUrl, file);
            HashMap map = Format.VehicleLicense(recognize);
            return R.ok(map);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

    @PostMapping("detect")
    public R detect(){
        try {
            DetectFace.detectFace();
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
        return R.ok();
    }

    @PostMapping("vedio")
    public R vedio(MultipartFile file) {
        try {
            Object bioAssayResult = ImageRecognize.getBioAssayResult(file);
            return R.ok(bioAssayResult);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

    @PostMapping("holiday")
    public R holiday(){
        HolidayRequest.holiday();
        return R.ok();
    }
}
